package com.web.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.biz.TMenutreeBIZ;
import com.entity.TMenutree;
import com.util.ReturnValue;

@Controller
public class TMenutreeController {
	private TMenutreeBIZ tMenutreeBIZ;
	@Resource
	public void settMenutreeBIZ(TMenutreeBIZ tMenutreeBIZ) {
		this.tMenutreeBIZ = tMenutreeBIZ;
	}
	
	@RequestMapping("/doAddMenutree.do")
	public void doAddMenutree(HttpServletRequest request,HttpServletResponse response) throws IOException{
		TMenutree tMenutree = new TMenutree();
		tMenutreeBIZ.save(tMenutree);
	}
	
	@SuppressWarnings("rawtypes")
	@RequestMapping("/doGetMenuTreeByParentId.do")
	public void doGetMenuTreeByParentId(HttpServletRequest request,HttpServletResponse response){
		String id= request.getParameter("id");
		id = id==null ? "0" : id;
		List tmp = this.tMenutreeBIZ.findByParentId(id);
		List<Map<String, String>> reList = new ArrayList<Map<String, String>>();
		for (int i = 0; i < tmp.size(); i++) {
			TMenutree tMenutree = (TMenutree) tmp.get(i);
			Map<String, String> map = new HashMap<String, String>();
			map.put("id", tMenutree.getId());
			map.put("pId",tMenutree.getParentId());
			map.put("name", tMenutree.getName());
			map.put("url",tMenutree.getUrl()==null?"":tMenutree.getUrl());
			List menuTrees = this.tMenutreeBIZ.findByParentId(tMenutree.getId());
			if(menuTrees.size()>0){
				map.put("isParent", "true");
			}
			reList.add(map);
		}
		
		try {
			ReturnValue.getResponse(response, reList);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
